0

I've see a lot posts about codeigniter and angularjs work together,but most of them are about retriving data and pass data .My question is more basic.

I run the site through php,then every thing is ok,means that angularjs work properly.But when I run it through controller,it didn't work.what's wrong?Here are parts of my codes:

<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";
});

my controller file is first.php,view file is first.php as well.when I browse the site by http://localhost/ci_ngtest/index.php/first, the first and last name input are blank,and the Full name show {{firstName + " " + lastName}} instead of John Doe


here is my project structure:

 --application
 --node_modules
   --angular
     --angular.js
   --angular-material
 --system
 --user_guide
 --somefiles(somefiles below)
9
  • Where you added angularjs library in your view ...? Commented Apr 19, 2016 at 5:52
  • in the body,<script src="../../node_modules/angular/angular.js"></script> Commented Apr 19, 2016 at 5:53
  • i also add <script src="../../node_modules/angular-material/angular-material.js"></script> Commented Apr 19, 2016 at 5:54
  • check in console what errors you are getting ..? Commented Apr 19, 2016 at 5:55
  • Failed to load:localhost/node_modules/angular/angular.js resource the server responded with a status of 404 (Not Found) Commented Apr 19, 2016 at 5:57

1 Answer 1

1

You need to configure script path properly:

  1. First set base_url inside application\config\config.php:

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

  1. load library inside application\config\autoload.php

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

  1. configure script path:

<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>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.