首页 文章

如何结合codeigniter和angularjs?

提问于
浏览
0

我看到很多关于codeigniter和angularjs的帖子一起工作,但大多数都是关于重新获取数据和传递数据 . 我的问题更基本 .

我通过php运行网站,然后每件事都没问题,意味着angularjs正常工作 . 但是当我通过控制器运行它时,它不起作用 . 有什么问题?这是我的代码的一部分:

<body ng-app="myApp" ng-controller="myCtrl">

First Name: <input type="text" ng-model="firstName"><br>
Last Name: <input type="text" ng-model="lastName"><br>
<br>
Full Name: {{firstName + " " + lastName}}

class First extends CI_Controller{

  public function index(){
      $this->load->view('first');
  }

}

var app = angular.module('myApp', ['ngMaterial']);
app.controller('myCtrl', function($scope) {
    $scope.firstName = "John";
    $scope.lastName = "Doe";
});

我的控制器文件是first.php,视图文件也是first.php . 当我按http://localhost/ci_ngtest/index.php/first浏览网站时,输入的姓名和空白名称都是空白的,全名显示{{firstName " " lastName}}而不是John Doe


这是我的项目结构:

--application
 --node_modules
   --angular
     --angular.js
   --angular-material
 --system
 --user_guide
 --somefiles(somefiles below)

1 回答

  • 0

    您需要正确配置脚本路径:

    • 首先在application \ config \ config.php中设置 base_url

    $config['base_url']= 'http://localhost/ci_ngtest/index.php/';

    • 在application \ config \ autoload.php中加载库

    $autoload['helper'] = array('url');

    • 配置脚本路径:

    <script src="<?php echo base_url();?>node_modules/angular/angular.js"></script> <script src="<?php echo base_url();?>node_modules/angular-material/angular-material.js"></script>

相关问题